package com.sc.springboot.controller;

import com.sc.springboot.bean.ResultBean;
import com.sc.springboot.bean.User;
import com.sc.springboot.service.IUserService;
import com.sc.springboot.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam("userName") String name, @RequestParam("passWord") String psw) {
        User user = new User();
        user.setUserName(name);
        user.setPassWord(psw);
        if (userService.add(user)) {
            return "0";
        }
        return "1";
    }

    /**
     * {
     * "request": {
     * "userName": "wangwu",
     * "passWord": 456
     * }
     * }
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add1", method = RequestMethod.POST)
    public ResultBean add(@RequestBody User user) {
        try {
            if (userService.add(user)) {
                return ResultUtil.forResult("0", null);
            }
            return ResultUtil.forResult("1", null);
        } catch (Exception e) {
            return ResultUtil.forResult("2", null);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultBean delete(@RequestBody User user) {
        try {
            User u = userService.findByUserName(user.getUserName());
            if (userService.delete(u)) {
                return ResultUtil.forResult("0", null);
            }
            return ResultUtil.forResult("1", null);
        } catch (Exception e) {
            return ResultUtil.forResult("2", null);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultBean update(@RequestBody User user) {
        try {
            String userName = user.getUserName();
            User result = userService.findByUserName(userName);
            User newUser = new User();
            newUser.setId(result.getId());
            newUser.setUserName(user.getUserName());
            newUser.setPassWord(user.getPassWord());
            if (userService.update(newUser)) {
                return ResultUtil.forResult("0", null);
            }
            return ResultUtil.forResult("1", null);
        } catch (Exception e) {
            return ResultUtil.forResult("2", null);
        }
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public ResultBean findAll() {
        try {
            List<User> userList = userService.findAll();
            return ResultUtil.forResult("0", userList);
        } catch (Exception e) {
            return ResultUtil.forResult("1", null);
        }
    }

    @RequestMapping(value = "/findUserByUserName", method = RequestMethod.POST)
    public ResultBean findByUserName(@RequestBody User user) {
        try {
            List<User> datas = new ArrayList<>();
            datas.add(userService.findByUserName(user.getUserName()));
            System.out.println(datas);
            return ResultUtil.forResult("0", datas);
        } catch (Exception e) {
            return ResultUtil.forResult("1", null);
        }
    }
}
